using System;
using System.Collections.Generic;
using System.Text;
using CMS.BIZ;
using AAM.DAL;
using AAM.BIZ;
using AAM.SQL;

namespace CMS.BOP
{
    public class SqlBizForumMessageProvider : SqlBizObjectProvider<BizForumMessage>
    {

        public BizForumMessage Read(int forumMessageID) { return Read(new ObjectID(BizForumMessage.ObjectType, new object[] { forumMessageID })); }
        public BizForumMessage Find(int forumMessageID) { return Find(new ObjectID(BizForumMessage.ObjectType, new object[] { forumMessageID })); }
        public bool Delete(int forumMessageID) { return Delete(new ObjectID(BizForumMessage.ObjectType, new object[] { forumMessageID })); }


        public List<BizForumMessage> ReadAllByPost(int? postID, string order)
        {
            Command command;
            string orderBy = " ORDER BY " + order;
            if (postID == null) command = Connection.CreateCommand("SELECT * FROM " + Table.TableName + " WHERE ForumPostID IS NULL" + orderBy);
            else
            {
                command = Connection.CreateCommand("SELECT * FROM " + Table.TableName + " WHERE ForumPostID=?" + orderBy);
                command.AddParameter(postID);
            }
            return CreateListFromCommand(command);
        }
        public List<BizForumMessage> ReadAllByPost(int? postID) { return ReadAllByPost(postID, "CreationDate"); }

        public int DeleteByPostID(int? postID)
        {
            Command command;
            if (postID == null) command = Connection.CreateCommand("DELETE FROM " + Table.TableName + " WHERE ForumPostID IS NULL");
            else
            {
                command = Connection.CreateCommand("DELETE FROM " + Table.TableName + " WHERE ForumPostID=?");
                command.AddParameter(postID);
            }
            int result = command.ExecuteNonQuery();
            command.Dispose();
            return result;
        }
    }
}
